<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\helpers\Json;

class BlogController extends Controller {
    public $layout = "@app/views/layouts/admin.php";
    public $enableCsrfValidation = false;
    public function actionIndex() {

        $connection = \Yii::$app->db;
        $ret = $connection->createCommand("select * from qbli_article order by addtime DESC")->queryAll();
        return $this->render('index',["ret"=>$ret]);
    }

    public function actionCreate() {
        return $this->render("_add");
    }

    public function actionAdd() {
        $aid = $_POST['aid'];
        $title = $_POST['title'];
        $content = $_POST['content'];
        $date = date('y-m-d h:i:s',time());
        $connection = \Yii::$app->db;
        if (empty($id)) {
            $connection->createCommand()->insert('qbli_article', [
               'aid' => 'null',
                'title' => $title,
                'addtime' => $date,
                'content' => $content,
            ])->execute();
        } else {
           $connection->createCommand("update qbli_article set title='$title',content='$content' where aid=$aid")->execute();
        }
        return  JSON::encode("success");
    }

    public function actionDelete() {
         $connection = \Yii::$app->db;
        $aid = $_GET['aid'];
        $sql = "Delete from qbli_article where aid = ". $aid;
        $connection->createCommand($sql)->execute();
        $ret = $connection->createCommand("select * from qbli_article")->queryAll();
        return $this->render('index',["ret"=>$ret]);
    }

    public function actionEdit() {
        $connection = \Yii::$app->db;
        $aid = $_GET['aid'];
        $ret = $connection->createCommand("select * from qbli_article where aid = $aid")->queryOne();
        return $this->render('_add',["ret"=>$ret]);
    }
}
